package com.wokeMy.view.base;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.bigkoo.alertview.AlertView;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;


public abstract class BaseActivity extends AppCompatActivity {
    long lastClick = 0;
    long passlastClick = 0;
    long yzmlastClick = 0;

    AlertDialog alertDialog;
    private AlertView mAlertView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
        initView();
        initData();
        initActivity();
    }

//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//        overridePendingTransition(R.anim.anim_slide_back_enter, R.anim.anim_slide_back_exit);
//    }
//public void tishi( String kll) {
//    alertDialog = new AlertDialog.Builder(BaseActivity.this).
//            setTitle("提示").
//            setMessage(kll).
//            setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    alertDialog.dismiss();
//                }
//            }).create();
//    alertDialog.show();
//}
public void tishi( String kll) {
    mAlertView=new AlertView("提示",kll, null, new String[]{"确定"}, null, BaseActivity.this, AlertView.Style.Alert,null);
    mAlertView.show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        if (mAlertView != null && mAlertView.isShowing()) {
            mAlertView.dismiss();
            return false;
        }
    }
    return super.onKeyDown(keyCode, event);

}
public void tishiToast( String kll) {
Toast.makeText(BaseActivity.this,kll,Toast.LENGTH_SHORT).show();
}
public Dialog createLoadDial() {
    return Util.createLoadingDialog(this);
}
public void closeLoadDial(Dialog dialog) {
    Util.closeDialog(dialog);
}
    /**
     * 查找组件
     *
     * @param viewId：View的ID
     * @return View
     */
    @SuppressWarnings("unchecked")
    protected <view extends View> view myFindViewById(int viewId) {
        return (view) findViewById(viewId);
    }

    /**
     * 启动Activity
     *
     * @param context：Context
     * @param cls：Class<?>
     * @param isFinish：是否关闭Activity
     */
    protected void startActivityByIntent(Context context, Class<?> cls, Boolean isFinish) {
        startActivity(new Intent(context, cls));
        if (isFinish) {
            finish();
        }
//        overridePendingTransition(R.anim.anim_slide_forword_enter, R.anim.anim_slide_forword_exit);
    }

    /**
     * 显示短Toast
     *
     * @param context：Context
     * @param text：String
     */
    protected void showShortToastByString(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示长Toast
     *
     * @param context：Context
     * @param text：String
     */
    protected void showLongToastByString(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }

    /**
     * 设置SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param value：值
     */
    protected void setSP(Context context, String key, int value) {
        SharedPreferences.Editor et = context.getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE).edit();
        et.putInt(key, value);
        et.apply();
    }

    /**
     * 设置SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param value：值
     */
    protected void setSP(Context context, String key, String value) {
        SharedPreferences.Editor et = context.getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE).edit();
        et.putString(key, value);
        et.apply();
    }

    /**
     * 设置SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param value：值
     */
    protected void setSP(Context context, String key, Boolean value) {
        SharedPreferences.Editor et = context.getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE).edit();
        et.putBoolean(key, value);
        et.apply();
    }

    /**
     * 获取SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param defaultValue：值
     * @return 值
     */
    protected int getSP(Context context, String key, int defaultValue) {
        SharedPreferences sp = context.getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE);
        return sp.getInt(key, defaultValue);
    }


    /**
     * 获取SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param defaultValue：值
     * @return 值
     */
    protected String getSP(Context context, String key, String defaultValue) {
        SharedPreferences sp = context.getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(key, defaultValue);
    }

    /**
     * 获取SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param defaultValue：值
     * @return 值
     */
    protected boolean getSP(Context context, String key, Boolean defaultValue) {
        SharedPreferences sp = context.getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE);
        return sp.getBoolean(key, defaultValue);
    }
    /**
     * 防止快速点击
     * @return
     */
    public boolean yzmfastClick() {
        if (System.currentTimeMillis() - yzmlastClick < 60000) {
            Toast.makeText(BaseActivity.this,"请一分钟后尝试！",Toast.LENGTH_SHORT).show();
            return true;
        }
        yzmlastClick = System.currentTimeMillis();
        return false;
    }
    /**
     * 防止快速点击
     * @return
     */
    public boolean fastClick() {
        if (System.currentTimeMillis() - lastClick < 1000) {
            Toast.makeText(BaseActivity.this,"请稍后尝试！",Toast.LENGTH_SHORT).show();
            return true;
        }
        lastClick = System.currentTimeMillis();
        return false;
    }
    /**
     * 防止快速点击 无提示
     * @return
     */
    public boolean fastClickNoToast() {
        if (System.currentTimeMillis() - lastClick < 1000) {
            return true;
        }
        lastClick = System.currentTimeMillis();
        return false;
    }
    /**
     * 防止通道快速点击
     * 接口通道回调 点击延迟处理
     * @return
     */
    public boolean PassfastClick() {
        if (System.currentTimeMillis() - passlastClick < 1000) {
//            Toast.makeText(BaseActivity.this,"请一分钟后尝试！",Toast.LENGTH_SHORT).show();
            return true;
        }
        passlastClick = System.currentTimeMillis();
        return false;
    }


    /**
     * 设置布局
     */
    public abstract void setContentView();


    /**
     * 初始化View
     */
    public abstract void initView();

    /**
     * 初始化数据
     */
    public abstract void initData();


    /**
     * 初始化Activity
     */
    public abstract void initActivity();

}